#include "bfg.h"
#include <ctime> 

static int test = 0;

CBFG::CBFG()
{
	velocity = CVector(0.0, 0.0, 120.0);
	acceleration = CVector(0.0, 0.0, 0.0);
	distanceTravel = 0.0;
	size = 1.0f;
	isExplosion = false;

	explosion = NULL;
	explosionTex = new CTexture;
	quadric = gluNewQuadric();
	Load();
}

CBFG::~CBFG()
{
	if (explosion != NULL)
	{
		explosion->KillSystem();
		delete [] explosion;
		explosion = NULL;
	}

	if (explosionTex != NULL)
	{
		delete explosionTex;
		explosionTex = NULL;
	}
}

void CBFG::SetupExplosionTexture()
{
	glGenTextures(1, &explosionTex->texID);
	glBindTexture(GL_TEXTURE_2D, explosionTex->texID);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, 4, explosionTex->width, explosionTex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, explosionTex->data);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 4, explosionTex->width, explosionTex->height, GL_RGBA, GL_UNSIGNED_BYTE, explosionTex->data);
}

void CBFG::OnAnimate(scalar_t deltaTime)
{
	float cosYaw = (float)cos(DEG2RAD(direction));
	float sinYaw = (float)sin(DEG2RAD(direction)); 
	float sinPitch = (float)sin(DEG2RAD(pitch));

	float speed = velocity.z * deltaTime;

	position.x += float(cosYaw)*speed;
	position.y += float(sinPitch)*speed;
	position.z += float(sinYaw)*speed;

	distanceTravel += position.Length();

	if (isExplosion)
		explosion->Update(deltaTime);

	if (!isExplosion)
	{
		if (distanceTravel >= 500000.0f)
		{
			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
			explosion = new CExplosion(10, position, 0.1f, explosionTex->texID);
		}
	}


}

void CBFG::OnCollision(CObject *collisionObject)
{
	if (!isExplosion)
	{
		if (typeid(*collisionObject) == typeid(CTerrain))
		{
			if (((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size >= position.y)
			{
				isExplosion = true;
				velocity = CVector(0.0, 0.0, 0.0);
				explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
				PlaySound();
			}
		
			// do explosion
		}
		if (typeid(*collisionObject) == typeid(COgroEnemy))
		{
			CEnemy *bob = (CEnemy*)collisionObject;
			bob->takeDamge(100);
			
			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
			explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
			PlaySound();
		}
		if (typeid(*collisionObject) == typeid(CSodEnemy))
		{
			CEnemy *bob = (CEnemy*)collisionObject;
			bob->takeDamge(150);

			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
			explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
			PlaySound();
		}
		if (typeid(*collisionObject) == typeid(CDroidEnemy))
		{
			CEnemy *bob = (CEnemy*)collisionObject;
			bob->takeDamge(150);

			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
			explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
			PlaySound();
		}
	}
}

void CBFG::OnDraw(CCamera *camera)
{

	GLfloat redDiffuseMaterial[] = {1.0, 0.0, 0.0}; //set the material to red
	GLfloat whiteSpecularMaterial[] = {1.0, 1.0, 1.0}; //set the material to white
	GLfloat greenEmissiveMaterial[] = {0.0, 1.0, 0.0}; //set the material to green
	GLfloat whiteSpecularLight[] = {1.0, 1.0, 1.0}; //set the light specular to white
	GLfloat blackAmbientLight[] = {0.0, 0.0, 0.0}; //set the light ambient to black
	GLfloat whiteDiffuseLight[] = {1.0, 1.0, 1.0}; //set the diffuse light to white
	GLfloat blankMaterial[] = {0.0, 0.0, 0.0}; //set the diffuse light to white
	GLfloat mShininess[] = {128}; //set the shininess of the material

	GLfloat lPosition[] = {position.x, position.y, position.z,1};

	//positioning the light randomly around the sphere so it looks like energy flow, it's not working how i thought it would
	srand((unsigned)time(0));
	for(int i=0;i<3;i++)
	{
		int negTest = (rand()%2);
		if(negTest==0)
		{
			int movement = (rand()%10);
			lPosition[i] += movement;
		}else{
			int movement = (rand()%10) * (-1);
			lPosition[i] += movement;
		}
	}

	glLightfv(GL_LIGHT0, GL_SPECULAR, whiteSpecularLight);
    glLightfv(GL_LIGHT0, GL_AMBIENT, blackAmbientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteDiffuseLight);
	glLightfv(GL_LIGHT0, GL_POSITION, lPosition);

	// if the rocket has not yet exploded, draw the rocket model
	if (!isExplosion)
	{
		
		glColor3f(1.0, 1.0, 1.0);
		glTranslatef(position.x, position.y, position.z);
		glRotatef(-direction, 0.0, 1.0, 0.0);
		glScalef(0.025f, 0.025f, 0.025f);

		glEnable (GL_LIGHTING);
		glEnable (GL_LIGHT0);

		
		glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, greenEmissiveMaterial);
		 
		gluSphere(quadric,50.0,10,10);

		glDisable((GL_LIGHTING));
		glDisable(GL_LIGHT0);
		RenderFrame(0);
		
	}
	// draw explosion
	else
	{
		// Phase 16 - take out fog commands until fog is defined
		// Phase 20 - Uncomment fog statements
		glDisable(GL_FOG);
		explosion->Render();
		glEnable(GL_FOG);
		// Phase 16 - End
	}
}


void CBFG::Load()
{
	/*CMD2Model::Load(
		MEDIA_DIR "models/rocketair.md2", 
		MEDIA_DIR "models/rocket.pcx"
	);*/
	explosionTex->LoadTexture( MEDIA_DIR "explosion.bmp");
	SetupExplosionTexture();
}

void CBFG::Unload()
{
}

void CBFG::OnPrepare()
{
	// perform collision detection from this entity with all other objects in world
	if (!isExplosion)
		ProcessCollisions(FindRoot());

	if (isExplosion)
	{
		// Phase 16 - Take out for now
		// Phase 19 - Uncomment the following
		if (explosion->IsDead())
		{
			explosion->KillSystem();
			delete explosion;
			explosion = NULL;
			isExplosion = false;

			isDead = true;
		}
	}
}
